package com.hw.rediscache.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @program: spring-data-redis-cache
 * @description: redis操作类，可以避免直接操作redis的异常处理
 * @author: hewei
 * @date: 2021-09-18 14:31
 **/

@Slf4j
//@ConditionalOnBean(value = RedisTemplate.class)
@ConditionalOnBean(name = "redisTemplate")
@Component
public class SimpleRedisUtil {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public boolean exists(String key) {
        boolean result = false;
        try {
            result = redisTemplate.hasKey(key);
        } catch (Exception exception) {
            log.error(">> redis[exists] 操作异常： ", exception);
        }
        return result;
    }

    public Object get(Object key) {
        Object object = null;
        try {
            object = redisTemplate.opsForValue().get(key);
        } catch (Exception exception) {
            log.error(">> redis[get] 操作异常： ", exception);
        }
        return object;
    }

    public void set(String key, Object value) {
        try {
            redisTemplate.opsForValue().set(key, value);
        } catch (Exception exception) {
            log.error(">> redis[set] 操作异常： ", exception);
        }
    }

}